<?php
/**
 * 首页
 * @version 1.0.0 (Thu Feb 23 13:49:18 GMT 2012)
 * @author ZhangHao
 */

class Detail extends CI_Controller {
	private $_data;

    public function __construct() {
		parent::__construct();
		$this->load->model('base_mdl', 'base');
    }

	public function index() {
		$aid = intval($_GET['aid']);

		$this->_data['album'] = $this->base->get_data('albums', array('id'=>$aid))->row_array();
		$this->_data['lists'] = $this->base->get_data('images', array('aid'=>$aid))->result_array();
		$this->db->query('UPDATE mt_albums SET hits=hits+1 WHERE id='.$aid);

		$this->_data['msgs'] = $this->base->get_data('msg', array('aid'=>$aid), '*', 0, 0, 'ctime DESC')->result_array();
		$this->_data['jian'] = $this->db->query('SELECT * FROM mt_albums WHERE state=1 ORDER BY RAND() LIMIT 5')->result_array();

		$this->load->view(THEME.'/header', $this->_data);
		$this->load->view(THEME.'/detail', $this->_data);
		$this->load->view(THEME.'/footer');
	}

	public function setCover() {
		if(!get_cookie('uid')) {
			exit('0');
		}
		$aid = intval($_GET['aid']);
		$id = intval($_GET['id']);

		$image = $this->base->get_data('images', array('id'=>$id))->row_array();

		$this->base->update_data('albums', array('id'=>$aid), array('cover'=>$image['local_url']));
		echo 1;
	}

	public function sendMsg() {
		$aid = intval($_POST['aid']);
		$rid = !empty($_POST['rid']) ? intval($_POST['rid']) : 0;

		$insert_data = array(
			'aid' 		=> $aid,
			'content'	=> $_POST['content'],
			'ctime'		=> date('Y-m-d H:i:s'),
			'ip'		=> $this->input->ip_address(),
		);

		if($rid) {
			$insert_data['rid'] = $rid;
		}

		$msg = "";
		if($this->base->insert_data('msg', $insert_data)) {
			$this->db->query('UPDATE mt_albums SET msgnum=msgnum+1 WHERE id='.$aid);
			
			$msg = "<li>
						<div><span class=\"time\">".date('m-d H:i')."</span></div>
						<div class=\"content\">".t2h(strip_tags($_POST['content']))."</div>
					</li>";
		}

		echo $msg;
	}

	public function msgList() {
		$aid = intval($_GET['aid']);

		$this->_data['lists'] = $this->base->get_data('msg', array('aid'=>$aid))->result_array();
	}

	public function on() {
		header("Content-type: text/html; charset=utf-8"); 
		$aid = intval($_GET['aid']);

		$this->_data['album'] = $album = $this->base->get_data('albums', array('id'=>$aid))->row_array();

		$imageData = file_get_contents($album['page_url']);
		$imageData = json_decode($imageData, true);

		if(empty($imageData['img'])) {
			exit('errr');
		} 		

		echo $album['title']."<br>";

		foreach($imageData['img'] as $img) {
			echo "<img src='".$img."' /><br>";
		}
	}
}